package com.lhd.qd.module.system.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhd.qd.common.base.BaseServiceImpl;
import com.lhd.qd.common.translator.DBTransRule;
import com.lhd.qd.common.translator.TransUtils;
import com.lhd.qd.common.util.JacksonUtils;
import com.lhd.qd.module.system.employee.dao.EmployeeMapper;
import com.lhd.qd.module.system.user.dao.UserMapper;
import com.lhd.qd.module.system.user.dao.UserOperateLogMapper;
import com.lhd.qd.module.system.user.model.converter.AbstractUserOperateLogConverter;
import com.lhd.qd.module.system.user.model.dataobj.UserOperateLogDO;
import com.lhd.qd.module.system.user.model.dto.UserOperateLogPageQuery;
import com.lhd.qd.module.system.user.model.vo.UserOperateLogDetailVO;
import com.lhd.qd.module.system.user.model.vo.UserOperateLogListVO;
import com.lhd.qd.module.system.user.service.UserOperateLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserOperateLogServiceImpl extends BaseServiceImpl<UserOperateLogMapper, UserOperateLogDO> implements UserOperateLogService {

    @Override
    public IPage<UserOperateLogListVO> pageUserOperateLog(UserOperateLogPageQuery query) {

        LambdaQueryWrapper<UserOperateLogDO> queryWrapper = Wrappers.<UserOperateLogDO>lambdaQuery()
                .eq(query.getUserId() != null, UserOperateLogDO::getUserId, query.getUserId());

        IPage<UserOperateLogDO> doPage = page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);

        IPage<UserOperateLogListVO> voPage = AbstractUserOperateLogConverter.INSTANCE.doPage2ListVOPage(doPage);

        TransUtils.trans(voPage.getRecords(), UserOperateLogListVO.class, new DBTransRule[]{
                new DBTransRule<>(
                        new String[]{"userId", "employeeId", "username"},
                        UserMapper.class,
                        new String[]{"id", "employee_id", "username"}
                )
        });

        TransUtils.trans(voPage.getRecords(), UserOperateLogListVO.class, new DBTransRule[]{
                new DBTransRule<>(
                        new String[]{"employeeId", "name"},
                        EmployeeMapper.class,
                        new String[]{"id", "name"}
                )
        });

        return voPage;
    }

    @Override
    public UserOperateLogDetailVO getUserOperateLogById(Long id) {

        UserOperateLogDetailVO vo = AbstractUserOperateLogConverter.INSTANCE.do2DetailVO(getById(id));

        return vo;
    }

    @Override
    public void saveUserOperateLog(Long userId, String operateDesc, String method, String url, Long ipv4, Object[] params, Object result) {

        UserOperateLogDO dataObj = new UserOperateLogDO();
        dataObj.setUserId(userId);
        dataObj.setOperateTime(LocalDateTime.now());
        dataObj.setOperateDesc(operateDesc);
        dataObj.setMethod(method);
        dataObj.setUrl(url);
        dataObj.setIpv4(ipv4);
        dataObj.setParams(JacksonUtils.toStr(params));
        dataObj.setResult(JacksonUtils.toStr(result));

        save(dataObj);
    }

}
